package com.liucc.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.liucc.gulimall.product.entity.CategoryEntity;
import com.liucc.gulimall.product.service.CategoryService;
import com.liucc.common.utils.PageUtils;
import com.liucc.common.utils.R;



/**
 * 商品三级分类
 *
 * @author liuchuangchuang
 * @email 2514573000@qq.com
 * @date 2023-11-25 18:43:56
 */
@RestController
@RequestMapping("product/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查找商品所有分类，以树形结构进行展示
     */
    @RequestMapping("/list/tree")
    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = categoryService.queryPage(params);
        List<CategoryEntity> treeDatas = categoryService.listWithTree();

        return R.ok().put("data", treeDatas);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    @RequestMapping("/updateSort")
    public R updateSort(@RequestBody CategoryEntity[] categorys){
        categoryService.updateBatchById(Arrays.asList(categorys));
        return R.ok();
    }

    /**
     * 删除(逻辑删除)
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        log.info("catIds:{}", catIds);
		categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
